$(function() {
	$("#page-search").live("pageinit", function(){
		var $page = $(this);
		$page.find('#flip-search-detail').change(setDetailVisibility).trigger("change");
		$page.find('form').submit(checkSubmit);
	});
	
	function checkSubmit(event){
		var $this = $(this),
			$controls = $this.find('input:not(:submit), select, textarea').filter(':visible[data-role!="slider"]'),
			isEmpty = true;
			
		$controls.each(function(){
			if($.trim($(this).val()) != '')
				isEmpty = false;			
		});
			
		if(isEmpty){
			$.mobile.showNotification("적어도 하나의 조건을 입력해주세요.");
			event.preventDefault();
			return false;
		}
	}
	
	function setDetailVisibility(){
		var $this = $(this);
		var $fields = $('.search-detail-field');
		var $controls = $fields.find('input, select, textarea');
		
		if($this.val() == 1){
			$controls.removeProp('disabled');
			$fields.show();
		}else{
			$fields.hide();
			$controls.prop('disabled', 'true');
		}
		
		$.mobile.fixedToolbars.show(true)
	}
});
